package cn.kgc.javaweb.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.dao.impl.UserDaoImpl;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.enums.CodeMsg;
import cn.kgc.javaweb.service.UserService;
import cn.kgc.javaweb.util.ResponseResult;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/9
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String username, String password) {

        User user = userDao.findByUserNameAndPassword(username, SecureUtil.md5(password));
        return user;

    }

    @Override
    public ResponseResult selectPage(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<User> users =  userDao.selectPage(userPageDTO);
            // 查询数据总条数
            Long  total =  userDao.selectTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(users);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            User user =  userDao.selectById(id);
            responseResult.setData(user);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult update(User usr) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.update(usr);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult add(User usr) {

        ResponseResult responseResult = ResponseResult.builder().build();
        //  1. 根据用户名查询用户信息
        User user  = userDao.selectByUserName(usr.getUsername());
        if(user==null){
            // 2.根据查询的结果 判定用户名是否可用
            // 用户名可用   执行添加
            try {
                userDao.add(usr);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }
        }else{
            // 用户名不可用  直接返回结果
            responseResult.setMsg(CodeMsg.USER_EXIST.getMsg());
            responseResult.setCode(CodeMsg.USER_EXIST.getCode());
        }

        return responseResult;
    }

    @Override
    public ResponseResult delete(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.delete(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult updatePwdById(String newPassword, Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();

        try {
            userDao.updatePwdById(newPassword,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }
}
